window.appModule.controller('kListControl', function ($scope,$http,$state,$stateParams,kQuestionListAPI,ngNotify,$cookies,$document) {
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });
    if(":baseId" != $stateParams.baseId){
        $cookies.putObject('baseId', $stateParams.baseId);
    }
    kQuestionListAPI.getIndustryQuestionsByBaseId({baseId:$cookies.getObject('baseId'),pageIndex:0,pageSize:10}, function(result){
        if(result.messageCode == "10000"){
            for (var i in result.questionList) {
                if("" == result.questionList[i].videoUrl || undefined == result.questionList[i].videoUrl){
                    if("" == result.questionList[i].imageUrl || undefined == result.questionList[i].imageUrl){
                        result.questionList[i].type = "";
                    }else{
                        result.questionList[i].type = "[图片]";
                    }
                }else{
                    result.questionList[i].type = "[视频]";
                }
            }
            $scope.questions = result.questionList;
            $scope.pageIndex = result.pageIndex+1;
            $scope.pageCount = result.pageCount;
            $cookies.putObject('isSearch', 0);
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });

    $scope.loadMore = function(){
        if($scope.pageIndex < $scope.pageCount){
            if(1 == $cookies.getObject('isSearch')){
                kQuestionListAPI.getIndustryQuestionsByBaseId({baseId:$cookies.getObject('baseId'),pageIndex:$scope.pageIndex,pageSize:10,questionOrAnswer:$scope.param}, function(result){
                    if(result.messageCode == "10000"){
                        for (var i in result.questionList) {
                            if("" == result.questionList[i].videoUrl || undefined == result.questionList[i].videoUrl){
                                if("" == result.questionList[i].imageUrl || undefined == result.questionList[i].imageUrl){
                                    result.questionList[i].type = "";
                                }else{
                                    result.questionList[i].type = "[图片]";
                                }
                            }else{
                                result.questionList[i].type = "[视频]";
                            }
                            $scope.questions.push(result.questionList[i]);
                        }
                    }else{
                        ngNotify.set(result.messageText, 'error');
                    }
                });
            }else{
                kQuestionListAPI.getIndustryQuestionsByBaseId({baseId:$cookies.getObject('baseId'),pageIndex:$scope.pageIndex,pageSize:10}, function(result){
                    if(result.messageCode == "10000"){
                        for (var i in result.questionList) {
                            if("" == result.questionList[i].videoUrl || undefined == result.questionList[i].videoUrl){
                                if("" == result.questionList[i].imageUrl || undefined == result.questionList[i].imageUrl){
                                    result.questionList[i].type = "";
                                }else{
                                    result.questionList[i].type = "[图片]";
                                }
                            }else{
                                result.questionList[i].type = "[视频]";
                            }
                            $scope.questions.push(result.questionList[i]);
                        }
                    }else{
                        ngNotify.set(result.messageText, 'error');
                    }
                });
            }
        }
        $scope.pageIndex = $scope.pageIndex+1;
    };
    $scope.myKeydown = function(e){
        var keycode = window.event?e.keyCode:e.which;
        if(keycode==13){
            $scope.search();
        }
    };
    $scope.search = function(){
        if(undefined == $scope.param || null == $scope.param){
            $scope.param = "";
        }
        kQuestionListAPI.getIndustryQuestionsByBaseId({baseId:$cookies.getObject('baseId'),pageIndex:0,pageSize:10,questionOrAnswer:$scope.param}, function(result){
            if(result.messageCode == "10000"){
                for (var i in result.questionList) {
                    if("" == result.questionList[i].videoUrl || undefined == result.questionList[i].videoUrl){
                        if("" == result.questionList[i].imageUrl || undefined == result.questionList[i].imageUrl){
                            result.questionList[i].type = "";
                        }else{
                            result.questionList[i].type = "[图片]";
                        }
                    }else{
                        result.questionList[i].type = "[视频]";
                    }
                }
                $scope.questions = result.questionList;
                $scope.pageIndex = result.pageIndex+1;
                $scope.pageCount = result.pageCount;
                $cookies.putObject('isSearch', 1);
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    $scope.clean = function(){
        $scope.param = "";
    };
    $scope.myVar = true;
    $scope.deleteId;
    $scope.deleteQuestion = function(id) {
        console.log(id);
        $scope.deleteId = id;
        $scope.myVar = !$scope.myVar;
    };

    $scope.deleteCanel = function() {
        $scope.myVar = !$scope.myVar;
    };

    //删除问题
    $scope.deleteConfirm = function () {
        kQuestionListAPI.deleteIndustryQuestion({questionId:$scope.deleteId}, function(result){
            ngNotify.set(result.message, 'info');
            if(1 == $cookies.getObject('isSearch')){
                kQuestionListAPI.getIndustryQuestionsByBaseId({baseId:$cookies.getObject('baseId'),pageIndex:0,pageSize:10,questionOrAnswer:$scope.param}, function(result){
                    if(result.messageCode == "10000"){
                        for (var i in result.questionList) {
                            if("" == result.questionList[i].videoUrl || undefined == result.questionList[i].videoUrl){
                                if("" == result.questionList[i].imageUrl || undefined == result.questionList[i].imageUrl){
                                    result.questionList[i].type = "";
                                }else{
                                    result.questionList[i].type = "[图片]";
                                }
                            }else{
                                result.questionList[i].type = "[视频]";
                            }
                        }
                        $scope.questions = result.questionList;
                        $scope.pageIndex = result.pageIndex+1;
                        $scope.pageCount = result.pageCount;
                    }else{
                        ngNotify.set(result.messageText, 'error');
                    }
                });
            }else{
                kQuestionListAPI.getIndustryQuestionsByBaseId({baseId:$cookies.getObject('baseId'),pageIndex:0,pageSize:10}, function(result){
                    if(result.messageCode == "10000"){
                        for (var i in result.questionList) {
                            if("" == result.questionList[i].videoUrl || undefined == result.questionList[i].videoUrl){
                                if("" == result.questionList[i].imageUrl || undefined == result.questionList[i].imageUrl){
                                    result.questionList[i].type = "";
                                }else{
                                    result.questionList[i].type = "[图片]";
                                }
                            }else{
                                result.questionList[i].type = "[视频]";
                            }
                        }
                        $scope.questions = result.questionList;
                        $scope.pageIndex = result.pageIndex+1;
                        $scope.pageCount = result.pageCount;
                    }else{
                        ngNotify.set(result.messageText, 'error');
                    }
                });
            }
        });
        $scope.myVar = !$scope.myVar;
    };


    kQuestionListAPI.getUserById({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.title = result.user.nickname;
            if(result.user.nickname.length > 8){
                result.user.nickname = result.user.nickname.substring(0,8)+"...";
            }else if(result.user.nickname.length == 0){
                result.user.nickname = result.user.phoneNumber;
            }
            if('' == result.user.imageUrl){
                result.user.imageUrl = "resource/images/UserAvatar.png";
            }
            $scope.user = result.user;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    kQuestionListAPI.getMenusByUserId({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.menus = result.menuList;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    $scope.goSecondMenu = function(menuId) {
        if(1 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("robotList");
                }
            });
        }else if(2 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("knowledgeList");
                }
            });
        }
    };
    $scope.goMenu = function(menuId) {
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                $state.go("menu");
            }
        });
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideDiv');
        if(div.style.display == 'block' && (event.target.id != "showDiv" && event.target.id != "showDivImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeDiv = function() {
        var div = document.getElementById('hideDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block' && (event.target.id != "showUserDiv" && event.target.id != "showUserImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeUserDiv = function() {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $scope.goChangePassowrd = function() {
        $state.go("changePassword");
    };
    $scope.logOut = function() {
        kQuestionListAPI.logOut({userId:$cookies.getObject('user').id}, function(result){
            if(result.messageCode == "10000"){
                $cookies.putObject('user', "");
                $state.go("loginQR");
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
});
